package com.tj.collection.controller.admin;

import com.fqgj.common.api.Page;
import com.fqgj.common.api.annotations.ParamsValidate;
import com.tj.collection.bean.ApiResponse;
import com.tj.collection.common.RequestLocalInfo;
import com.tj.collection.controller.admin.request.AddRoleMenuVo;
import com.tj.collection.controller.admin.request.AddRoleVo;
import com.tj.collection.controller.admin.response.AdminRoleIdsResponse;
import com.tj.collection.controller.admin.response.RoleListResponse;
import com.tj.collection.db.domain.Role;
import com.tj.collection.db.service.AdminRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sm
 * Date: 2017/3/14
 * Time: 下午1:13
 */
@RequestMapping("/admin/role")
@RestController
public class RoleController {

    @Autowired
    private AdminRoleService adminRoleService;

    /**
     * 添加角色
     *
     * @param addRoleVo
     * @return
     */
    @RequestMapping("/add")
    @ParamsValidate
    public ApiResponse add(@RequestBody AddRoleVo addRoleVo) {
        adminRoleService.add(new Role(addRoleVo.getName(), addRoleVo.getDescription(), RequestLocalInfo.getCurrentAdmin().getMerchantCode()));
        return new ApiResponse<>();
    }

    /**
     * 添加角色和对应的菜单
     *
     * @param addRoleVo
     * @param addRoleMenuVo
     * @return
     */
    @RequestMapping("/addroleandmenu")
    @ParamsValidate
    public ApiResponse add(@RequestBody AddRoleVo addRoleVo, @RequestBody AddRoleMenuVo addRoleMenuVo) {
        adminRoleService.addRoleAndMenu(new Role(addRoleVo.getName(), addRoleVo.getDescription(), RequestLocalInfo.getCurrentAdmin().getMerchantCode()), addRoleMenuVo.getMenuIds());
        return new ApiResponse<>();
    }

    /**
     * 角色列表
     *
     * @param addRoleVo
     * @param page
     * @return
     */
    @RequestMapping("/list")
    public ApiResponse<RoleListResponse> list(@RequestBody AddRoleVo addRoleVo, Page page) {
        List<Role> roleList = adminRoleService.searchRoleList(addRoleVo.getName(), page, RequestLocalInfo.getCurrentAdmin().getMerchantCode());
        return new ApiResponse<>(new RoleListResponse(roleList, page));
    }

    /**
     * 更新角色
     *
     * @param roleId
     * @param addRoleVo
     * @return
     */
    @RequestMapping("/{roleId}/update")
    @ParamsValidate
    public ApiResponse update(@PathVariable Long roleId, @RequestBody AddRoleVo addRoleVo) {
        adminRoleService.update(new Role(roleId, addRoleVo.getName(), addRoleVo.getDescription(), RequestLocalInfo.getCurrentAdmin().getMerchantCode()));
        return new ApiResponse<>();
    }

    @RequestMapping("/{adminId}/ids")
    public ApiResponse userRoleIds(@PathVariable Long adminId) {
        List<Long> roleIds = adminRoleService.getRoleIdsByAdminId(adminId);
        return new ApiResponse<>(new AdminRoleIdsResponse(roleIds));
    }


}
